﻿var onekeyUserInfor; //一键委托信息
var saveInforName;//针对跳转城市保存选择金额名称
var onekeyUserInforLoanMoney = { 'daikuan': 10, 'daikuan1': 10, 'daikuanzixun': 10, 'onekey': 10, 'evaluate': 10, 'jingjian': 10, 'wanquan': 10, 'yixin': 10, 'ceping': 10, 'kefu': 10 };
var url_checkcity = ['lp/jingjian/','lp/wanquan','lp/yixin','lp/ceping','lp/kefu'];//填资料不必首先校验城市的url
var isCheckCity = true; //是否首先校验城市
var wanquan_utm_27 = false; //  lp/wanquan?utm_source=27;

if (typeof (document.URL.split('/lp/')[1]) != 'undefined') {
    saveInforName = document.URL.split('/lp/')[1].split('?')[1] ? document.URL.split('/lp/')[1].split('?')[0] : document.URL.split('/lp/')[1].split('/')[0];
} else {
    saveInforName = 'jingjian';
}
onekeyUserInfor = { 'LoanMoney': 10 }
if (Storage.Cookies.get('onekeyUserInforLoanMoney') != '' && saveInforName != undefined) {
    saveInforName = saveInforName.toLowerCase();
    onekeyUserInfor.LoanMoney = JSON.parse(Storage.Cookies.get('onekeyUserInforLoanMoney'))[saveInforName];
}
var userInfor = {}; // 用户信息
var boolNeedCode = true;
var UtmSourceArr = UtmSourceArr || [21, 26, 27, 28, 29, 30, 31, 32];

$(function () {
    //初始化
    //QueryString.Initial();
    //var linkSource = QueryString.GetValue("hmsr") || "";
    var linkSource = localStorage.getItem('LinkSource') || '';
    userInfor = Storage.get('userInfor') || {
        Token: "", Account: "", Pwd: "", Code: "", LoginType: 1,
        UserID: '', UserName: "", Nickname: "", Gender: 1, LinkSource: "", cityDefault: 0,
        CityInfor: { CityName: "请选择您工作生活的城市" }
    };

    $.each(url_checkcity, function (i) {
        if (pageUrl.indexOf(url_checkcity[i]) >= 0) {
            isCheckCity = false;
        }
    });
    var pageName = window.location.pathname.split('/')[2].toLowerCase()
    if (pageName == 'daikuan1' || pageName == 'daikuanzixun' || pageName == 'evaluate' || pageName == 'onekey') {
        isCheckCity = false;
    }

    var curCityInfor = Storage.get('userInfor') || { Token: "", Account: "", Pwd: "", Code: "", LoginType: 1, UserID: '', UserName: "", Nickname: "", Gender: 1, LinkSource: "", cityDefault: 0, CityInfor: { CityId: '', CityName: "", CityPinyin: "", CitySubstationID: '' } };

    //城市定位
    if (isCheckCity) {
        if (typeof (position_null_utm) == "undefined" || position_null_utm == true) {
            position('.city-text-layer', '/' + pageUrl, '/citylist?return=' + pageUrl + '_city_');
        };

    } else {
        if (LocalResult.UseUrlLoc && LocalResult.HasUrlCity && !isCheckCity) { //url带城市  
            allCities.forEach(function (item, i) {
                if (item.QuanPin.toLowerCase() == LocalResult.UrlCity.toLowerCase()) {
                    curCityInfor.CityInfor = {
                        CityId: item.CityID,
                        CityName: item.CityName,
                        CityPinyin: item.QuanPin,
                        CitySubstationID: item.CityID
                    };
                };
            });
            Storage.put('userInfor', curCityInfor);
            userInfor = Storage.get('userInfor');
        }
    }

    if (Storage.get('userInfor')) {
        if (Storage.get('userInfor').LoanMoney) {
            onekeyUserInfor.LoanMoney = Storage.get('userInfor').LoanMoney;
        };
    } else {
        onekeyUserInfor.LoanMoney = 10;
    };
    if (linkSource !== "") userInfor.LinkSource = linkSource;
    postData.Head.Token = userInfor.Token;
    if (LocalResult.LocFail == true) {
        localStorage.setItem('LocalResult', 'fail');
    } else {
        localStorage.removeItem('LocalResult');
    };
    Storage.put('userInfor', userInfor);
    var localUserInfor = JSON.parse(localStorage.getItem('userInfor'))
    if (localUserInfor && localUserInfor.CityInfor && localUserInfor.CityInfor.CityName){
        userInfor.CityInfor = localUserInfor.CityInfor
        Storage.put('userInfor', userInfor);
        localStorage.removeItem('userInfor')
    }
    if (userInfor && userInfor.CityInfor && userInfor.CityInfor.CityName) {
        $("#currentCity").text(userInfor.CityInfor.CityName);
        if (userInfor.CityInfor.CityName != '请选择您工作生活的城市') {
            $("#currentCity").css('color', '#000');
            $(".location .tips").show();
        };
    };

    if (userInfor && userInfor.Token) {
        //$(".one-key-apply #imgCode").hide();
        //$(".one-key-apply #msgCode").hide();
        $(".one-key-apply .list-group-item").hide();
        $(".agreeInfoBox").hide();
    };

    //注册来源
    var myRegister_source;
    if (window.location.pathname.indexOf("evaluate") > 0) {
        myRegister_source = 27;
    } else if (window.location.pathname.indexOf("jingjian") > 0) {
        myRegister_source = 34;
    } else if (window.location.pathname.indexOf("wanquan") > 0) {
        myRegister_source = 35;
    } else if (window.location.pathname.indexOf("yixin") > 0) {
        myRegister_source = 36;
    } else if (window.location.pathname.indexOf("ceping") > 0) {
        myRegister_source = 37;
    } else if (window.location.pathname.indexOf("kefu") > 0) {
        myRegister_source = 38;
    } else {
        myRegister_source = 17;
    }

    if (!!userInfor) {
        onekeyUserInfor = {
            Account: userInfor.Account,
            "Identity": null,
            "IdentityName": null,
            "UserName": userInfor.UserName || userInfor.Nickname,
            "UserID": userInfor.UserID,
            "Gender": userInfor.Gender || 1,
            "LoanMoney": userInfor.LoanMoney || '请选择您的贷款金额',
            "CellPhoneNumber": userInfor.Account,
            "SmsCode": "",
            "RegisterSource": myRegister_source,
            "CityId": userInfor.CityInfor.CityId,
            "LinkSource": userInfor.LinkSource,
            LoginSource: 10,
            CitySubstationID: userInfor.CityInfor.CityId
        };
        if (config.appType == 6) {
            onekeyUserInfor.LoanMoney = userInfor.LoanMoney || 10;
        };
    } else {
        onekeyUserInfor = {
            Account: "",
            "UserName": "",
            "UserID": "",
            "Gender": 1,
            "Identity": null,
            "IdentityName": null,
            "LoanMoney": userInfor.LoanMoney || '请选择您的贷款金额',
            "CellPhoneNumber": "",
            "SmsCode": "",
            "RegisterSource": myRegister_source,
            "CityId": userInfor.CityInfor.CityId,
            "LinkSource": "",
            LoginSource: 10,
            CitySubstationID: userInfor.CityInfor.CityId
        };
    };

    if (!!onekeyUserInfor.Account && onekeyUserInfor.Account != '' && onekeyUserInfor.Account != 'undefined') {
        $('.name,.tel,.msgCode').hide();
        boolNeedCode = false;
    };
    if (onekeyUserInfor.UserName) {
        $("#userName").val(onekeyUserInfor.UserName);
    };
    $("#sex").find("button").eq(2 - onekeyUserInfor.Gender).removeClass("active").siblings().addClass("active");
    if (config.appType == 6) {
        $("#userNeedMoney").text('我要贷款' + onekeyUserInfor.LoanMoney + '万元').css('color', '#000');
    } else if (config.appType == 4) {
        if (!isCheckCity) {
            if (onekeyUserInfor.LoanMoney == "请选择您的贷款金额") {
                onekeyUserInfor.LoanMoney = "10"
            }
            $("#userNeedMoney").text('我要贷款' + onekeyUserInfor.LoanMoney + '万元').css('color', '#000');
        } else {
            $("#userNeedMoney").text(onekeyUserInfor.LoanMoney);
        }
    };

    //$('#cellPhoneNumber').val(onekeyUserInfor.CellPhoneNumber);    

    //金额选择框
    $('#showMoneyBox').on('click', function () {

        if (pageUrl && (onekeyUserInfor.CityId == '' || onekeyUserInfor.CityId == undefined) && isCheckCity) {
            msgTip('请选择城市');
            return;
        } else {
            $('#selectMoney').show();
            $("#selectMoney").find("li").removeClass("color-red");
            if ($("#modelli" + onekeyUserInfor.LoanMoney) && $("#modelli" + onekeyUserInfor.LoanMoney).length > 0) {
                $("#modelli" + onekeyUserInfor.LoanMoney).addClass("color-red");
            };
            //else {
            //    $("#moneyInput").val(onekeyUserInfor.LoanMoney);
            //}
        };
    });

    //姓名输入框获得焦点
    $('#userName').on('click', function () {
        if (pageUrl && (onekeyUserInfor.CityId == '' || onekeyUserInfor.CityId == undefined) && isCheckCity) {
            msgTip('请选择城市');
            $(this).attr('readonly', 'readonly');
            $(this).attr('unselectable', 'on');
            return;
        } else if (pageUrl && onekeyUserInfor.CityId != '') {
            $(this).removeAttr('readonly');
            $(this).removeAttr('unselectable');
            $(this).focusin();
        };
    });
    $('#userName').on('focus', function () {
        if (pageUrl && (onekeyUserInfor.CityId == '' || onekeyUserInfor.CityId == undefined) && isCheckCity) {
            msgTip('请选择城市');
            $(this).attr('readonly', 'readonly');
            $(this).attr('unselectable', 'on');
            return;
        } else {
            if (pageUrl) {
                $(this).removeAttr('readonly');
                $(this).removeAttr('unselectable');
                $(this).focusin();
            };
            window.scrollTo(0, 200);
        };
    });
    //是否选择申请有礼
    //$('#isAcceptGift').on('click', function () {
    //    $(this).toggleClass('notGift');
    //});
    //手机号码输入11位之后，显示获取短信验证码按钮
    $('#cellPhoneNumber').on('click', function () {
        if (pageUrl && (onekeyUserInfor.CityId == '' || onekeyUserInfor.CityId == undefined) && isCheckCity) {
            msgTip('请选择城市');
            $(this).attr('readonly', 'readonly');
            $(this).attr('unselectable', 'on');
            return;
        } else {
            if (pageUrl) {
                $(this).removeAttr('readonly');
                $(this).removeAttr('unselectable');
            };
        };
    });
    $('#cellPhoneNumber').on('keyup', function () {
        if (pageUrl && (onekeyUserInfor.CityId == '' || onekeyUserInfor.CityId == undefined) && isCheckCity) {
            msgTip('请选择城市');
            $(this).attr('readonly', 'readonly');
            $(this).attr('unselectable', 'on');
            return;
        } else {
            if (pageUrl) {
                $(this).removeAttr('readonly');
                $(this).removeAttr('unselectable');
            };
            if ($(this).val().length == 11) {
                if ($('#codeImg_2').length > 0) {
                    $('#codeImg_2,#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());
                } else {
                    $('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());
                }
                $('#imgCode').show();
                $('#inputImgCode').focus();
                $('#msgCode').show();
            };
        };
    });

    //刷新图片验证码
    $('#refreshCode').on('click', function () {
        if ($('#codeImg_2').length > 0) {
            $('#codeImg_2,#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
        } else { 
            $('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());

        }
    });   


    //根据utmsource显示/隐藏1万，3万选项
    if (config.appType == 4) {
        var utm = parseInt(getUtmSource());
        if (utm) {
            if (UtmSourceArr.indexOf(utm) > -1) {
                $('#modelli1,#modelli3').hide();
                if (wanquan_utm_27) {
                    $('#selectMoneyBox h4').html('暂不接受7万以下的贷款申请');
                } else {
                    $('#selectMoneyBox h4').html('暂不接受5万以下的贷款申请');
                }
            } else {
                $('#modelli1,#modelli3').show();
                $('#selectMoneyBox h4').html('请选择贷款金额');
            };
        } else {
            $('#modelli1,#modelli3').show();
        };
    };
    var utmNew = parseInt(getUtmSource());
    if (utmNew) {
        if (UtmSourceArr.indexOf(utmNew) > -1) {
            var my_fromMinNum; //最小金额
            if (wanquan_utm_27) {
                my_fromMinNum = 7;
            } else {
                my_fromMinNum = 5;
            }
            if ($('#rangeIpt').length > 0 && Storage.Cookies.get('onekeyUserInforLoanMoney') == '') {
                LoanMoneySlider(5, 35, my_fromMinNum);
            } else if ($('#rangeIpt').length > 0 && Storage.Cookies.get('onekeyUserInforLoanMoney') != '') {
                LoanMoneySlider(5, 35, my_fromMinNum, JSON.parse(Storage.Cookies.get('onekeyUserInforLoanMoney'))[saveInforName]);
            }
        } else {
            if ($('#rangeIpt').length > 0 && Storage.Cookies.get('onekeyUserInforLoanMoney') == '') {
                LoanMoneySlider(0, 30, 1);
            } else if ($('#rangeIpt').length > 0 && Storage.Cookies.get('onekeyUserInforLoanMoney') != '') {
                LoanMoneySlider(0, 30, 1, JSON.parse(Storage.Cookies.get('onekeyUserInforLoanMoney'))[saveInforName]);
            };
        };
    } else {
        if ($('#rangeIpt').length > 0 && Storage.Cookies.get('onekeyUserInforLoanMoney') == '') {
            LoanMoneySlider(0, 30, 1);
        } else if ($('#rangeIpt').length > 0 && Storage.Cookies.get('onekeyUserInforLoanMoney') != '') {
            LoanMoneySlider(0, 30, 1, JSON.parse(Storage.Cookies.get('onekeyUserInforLoanMoney'))[saveInforName]);
        };
    };

});

//有城市时去除readonly
window.onload = function () {
    if (pageUrl && onekeyUserInfor.CityId != '' && onekeyUserInfor.CityId != undefined) {
        $('#userName').removeAttr('readonly');
        $('#userName').removeAttr('unselectable');
        $('#cellPhoneNumber').removeAttr('readonly');
        $('#cellPhoneNumber').removeAttr('unselectable');
    };
};

//提示信息
function msgTip(Msg) {
    $("#msgPop").show();
    $("#msgPop").css("top", "65%");
    $("#msgTip").text(Msg);
    setTimeout(function () { $("#msgPop").hide(); }, 2000);
};

//金额选择
function clickOK(a) { //传入用户选择或输入的金额
    if (a == -1) {
        a = $("#moneyInput").val();
        $("#moneyInput").val("");
    };
    if (a === "" || a == 0) {
        msgTip("请输入大于0的数字")
        return;
    };
    var pat2 = /^[0-9]*$/;
    if (!pat2.test(a)) {
        msgTip("请输入正整数");
        return;
    };
    if (wanquan_utm_27 && a<7) {
        msgTip('该平台不接受7万以下的贷款申请！');
        return;
    } else {
        if (UtmSourceArr.indexOf(parseInt(getUtmSource())) > -1 && a < 5 && config.appType == 4) {
            msgTip('该平台不接受5万以下的贷款申请！');
            return;
        };
    };
    
    onekeyUserInfor.LoanMoney = Number(a);

    $("#moneyInput").blur();
    $('#selectMoney').hide();
    $("#userNeedMoney").text('我要贷款' + onekeyUserInfor.LoanMoney + '万元').css('color', '#000');

    if ($('#rangeIpt').length > 0) {
        var maxnum = parseInt($('.rangeBox .js-grid-text-5').text()) + 5;
        if (a < maxnum) {
            $rangeIpt.update({
                from: a,
                onUpdate: function (data) {
                    $('.irs-slider.single').append('<span class="imgBtn"></span>');
                    $('.rangeBox .js-grid-text-6').html('其他');
                    $('.rangeBox .irs-bar-edge').before('<span class="irs-bar-edge-pre"></span>');
                }
            });
        } else {
            $('.rangeBox .irs-single').html('<span>金额</span>' + a + '万');
        };
    };//贷款金额滚动条

    window.scrollTo(0, 1);
}

function maxFunc(maxName, maxNum) {
    $(maxName).val($(maxName).val().replace(/[^\d]/g, ''));
    if ($(maxName).val().length > 0) {
        $(maxName).val(Number($(maxName).val()) + '')
    };
    if ($(maxName).val() > maxNum) {
        $(maxName).val('300');
    };
}

//性别选择
function clickGender(e, gender) {
    if (pageUrl && onekeyUserInfor.CityId == '' && isCheckCity) {
        msgTip('请选择城市');
        return;
    } else {
        if ($("#Gender .btn").length > 0) {
            $("#Gender .btn").removeClass("active");
            $(e).addClass("active");

        } else {
            $("#Gender button").removeClass("active");
            $(e).addClass("active");
        };
        onekeyUserInfor.Gender = gender;
    };
};

//获取短信验证码
function getCode() {
    var checkTelObj = checkTel($("#cellPhoneNumber").val());
    if (!checkTelObj.isPass) {
        msgTip(checkTelObj.errorMsg);
        return;
    };

    var handler = { flag: true };
    if ($('#inputImgCode').length != 0) {
        $("#inputImgCode").val($("#inputImgCode").val().replace(/(^\s*)|(\s*$)/g, ""));
        var reg = /^[A-Za-z0-9]{4}$/;
        var checkImgCodeObj = $("#inputImgCode").val();

        if (checkImgCodeObj.length == 0) {

            msgTip('图片验证码不能为空');
            $('#inputImgCode').focus();
            return;
        };
        if (!reg.test(checkImgCodeObj)) {

            msgTip('图片验证码输入有误');
            $('#inputImgCode').val('');
            if ($('#codeImg_2').length > 0) {
                $('#codeImg_2,#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
            } else {
                $('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());

            }
            $('#inputImgCode').focus();
            return;
        };
        var callback = function () {
            if ($('#codeImg_2').length > 0) {
                $('#codeImg_2,#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
            } else {
                $('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());

            }
        };
    };
    $('#inputSmsCode').focus();
    if (callback) {
        fungetCode(handler, callback);
    } else {
        fungetCode(handler);
    };
    var Guid = getCookie('ValidateGuid');
    onekeyUserInfor.CellPhoneNumber = $("#cellPhoneNumber").val();
    postData.Content = { "Tel": onekeyUserInfor.CellPhoneNumber, 'ValidateCode': checkImgCodeObj || '', 'Guid': Guid };
    postData.Head.AppType = config.appType;
    $.ajax({
        type: 'POST', dataType: 'json',
        url: '/apply/GetSmsCodeFromProduct',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: JSON.stringify(postData),
        contentType: 'application/json',
        error: function (data, status, headers, config) {
            msgTip(status);
        },
        success: function (data) {
            if (data.Head.Ret == -1) {
                if (data.Head.Code == 105022) {
                    var ImgCode = [];
                    ImgCode.push('<div class="imgCode" id="imgCode">');
                    ImgCode.push('<input type="text" placeholder="请输入图片验证码" maxlength="4" id="ImgCode"/>');
                    ImgCode.push('<p id="refreshCode" class="imgcode clearfix">');
                    ImgCode.push('<img  id="codeImg" class="fl"/>');
                    ImgCode.push('<span class="fl"></span>');
                    ImgCode.push('</p>');
                    ImgCode.push('</div>');
                    var ImgCode = ImgCode.join('\n');
                    $('#msgCode').before(ImgCode);
                    if ($('#codeImg_2').length > 0) {
                        $('#codeImg_2,#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
                    } else {
                        $('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());

                    }
                    $('#inputImgCode').css('display', 'block');

                    handler.flag = false;
                    msgTip(data.Head.Msg);
                    $('#inputImgCode').focus();
                } else {
                    msgTip(data.Head.Msg);
                    handler.flag = false;
                    if ($('#codeImg_2').length > 0) {
                        $('#codeImg_2,#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
                    } else {
                        $('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());

                    }
                    //if (data.Head.Code != 105019) {
                    //    handler.flag = true;
                    //}
                    $('#inputImgCode').val('');
                    $('#inputImgCode').focus();
                };
            } else {
                handler.flag = true;
                //$('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());
                //$('#inputImgCode').val('');
            };
        }
    });
    postData.Content = { "CellPhone": onekeyUserInfor.CellPhoneNumber, "CitySubstationID": onekeyUserInfor.CitySubstationID, "RegisterSourceEnum": onekeyUserInfor.RegisterSource, "Gender": onekeyUserInfor.Gender, "LoanAmount": onekeyUserInfor.LoanMoney, "LinkSource": onekeyUserInfor.LinkSource, "RealName": $("#userName").val() };
    ; postData.Head.AppType = config.appType;
    $.ajax({
        type: 'POST', dataType: 'json',
        url: '/Account/PreReg',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: JSON.stringify(postData),
        contentType: 'application/json',
        error: function (data, status, headers, config) {
            showTip(status);
        }
    });
};

//一键委托
function oneKeySubmit() {
    //if (!$('#isAcceptGift').hasClass('notGift')) {
    //    Storage.Cookies.put('isAcceptGift', JSON.stringify({ isAcceptGift: true, pathName: location.pathname }));

    //} else {
    //    Storage.Cookies.put('isAcceptGift', JSON.stringify({ isAcceptGift: false, pathName: location.pathname }));
    //};
    if (userInfor && userInfor.Token) {
        assessEnter();
        return;
    };

    if (!onekeyUserInfor.Account || onekeyUserInfor.Account == 'undefined' || onekeyUserInfor.Account === '') {
        //var reg = /^[0-9]*$/;
        //var loanMoney = $("#loanMoney").val();
        //if (loanMoney === '' || loanMoney == 0) {
        //    msgTip('贷款金额请输入大于0的数字');
        //    return;
        //};
        //if (!reg.test(loanMoney)) {
        //    msgTip('贷款金额请输入正整数');
        //    $("#loanMoney").val('');
        //    return;
        //};
        //if (getUtmSource() == 21 && loanMoney < 5) {
        //    msgTip('该平台不接受5万以下的贷款申请！');
        //    return;
        //};
        //onekeyUserInfor.LoanMoney = loanMoney;
        if (onekeyUserInfor.CityId == '' || onekeyUserInfor.CityId == undefined) {
            msgTip('请选择城市');
            return;
        };

        if (onekeyUserInfor.LoanMoney == '请选择您的贷款金额') {
            msgTip('请选择您的贷款金额');
            return;
        };

        var checkNicknameObj = checkNickname($("#userName").val());
        if (!checkNicknameObj.isPass) {
            msgTip(checkNicknameObj.errorMsg);
            return;
        }
        onekeyUserInfor.UserName = $("#userName").val();

        var checkTelObj = checkTel($("#cellPhoneNumber").val());
        if (!checkTelObj.isPass) {
            msgTip(checkTelObj.errorMsg);
            return;
        };
        onekeyUserInfor.CellPhoneNumber = $("#cellPhoneNumber").val();

        if ($('#liImgCode').length != 0) {
            var reg = /^[A-Za-z0-9]{4}$/;
            var checkImgCodeObj = $("#inputImgCode").val();
            if (checkImgCodeObj.replace(/(^\s*)|(\s*$)/g, "").length == 0) {
                clearInterval();
                msgTip('请输入图片验证码');
                return;
            };
            if (!reg.test(checkImgCodeObj.replace(/(^\s*)|(\s*$)/g, ""))) {
                clearInterval();
                msgTip('您输入的图片验证码有误');
                $('#inputImgCode').val('');
                if ($('#codeImg_2').length > 0) {
                    $('#codeImg_2,#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
                } else {
                    $('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());

                }
                return;
            };
        };

        var checkCodeObj = checkCode($("#inputSmsCode").val());
        if (!checkCodeObj.isPass) {
            msgTip(checkCodeObj.errorMsg);
            return;
        };
        onekeyUserInfor.SmsCode = $("#inputSmsCode").val();

    };
    if (boolNeedCode == true) {
        if ($('#liImgCode').length != 0) {
            var reg = /^[A-Za-z0-9]{4}$/;
            var checkImgCodeObj = $("#inputImgCode").val();
            if (checkImgCodeObj.replace(/(^\s*)|(\s*$)/g, "").length == 0) {
                clearInterval();
                msgTip('请输入图片验证码');
                return;
            };
            if (!reg.test(checkImgCodeObj.replace(/(^\s*)|(\s*$)/g, ""))) {
                clearInterval();
                msgTip('您输入的图片验证码有误');
                //$('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
                //$('#inputImgCode').val('');
                return;
            };
        };

        var checkCodeObj = checkCode($("#inputSmsCode").val());
        if (!checkCodeObj.isPass) {
            msgTip(checkCodeObj.errorMsg);
            return;
        };
        onekeyUserInfor.SmsCode = $("#inputSmsCode").val();
    };

    onekeyUserInfor.FromWebSite = getFromWebSite();
    postData.Content = onekeyUserInfor;

    if ($("#oneKeyBtnSubmit")[0].nodeName == 'button' || $("#oneKeyBtnSubmit")[0].nodeName == 'BUTTON') {
        $("#oneKeyBtnSubmit").text("正在提交...").attr('disabled', true);
    } else {
        $("#oneKeyBtnSubmit").val("正在提交...").attr('disabled', true);
    };

    $.ajax({
        type: 'POST', dataType: 'json',
        url: '/apply/onekeyapply',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: JSON.stringify(postData),
        contentType: 'application/json',
        error: function (data, status, headers, config) {
            msgTip(status);
        },
        success: function (data) {
            //$("#oneKeyBtnSubmit").text("一键委托").removeAttr('disabled');
            var token = data.Content ? data.Content.Token : '',
                isLogin = data.Content ? data.Content.IsLogin : '';
            boolNeedCode = false;
            if (data.Head.Ret == -1) {
                if ($("#oneKeyBtnSubmit").hasClass('btn_daikuan')) {
                    $("#oneKeyBtnSubmit").val("马上申请").removeAttr('disabled');
                } else {
                    $("#oneKeyBtnSubmit").val("极速贷款").removeAttr('disabled');
                };
                $('.name,.tel,.msgCode').show();
                msgTip(data.Head.Msg);
                if (data.Head.Code == 105000 || data.Head.Code == 10003) {
                    $("#cellPhoneNumber").val(onekeyUserInfor.CellPhoneNumber);
                    onekeyUserInfor.SmsCode = "";
                    $("#inputSmsCode").val("");
                    $('#inputImgCode').val('');
                    if ($('#codeImg_2').length > 0) {
                        $('#codeImg_2,#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
                    } else {
                        $('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds());

                    }
                    boolNeedCode = true;
                };
                if (data.Head.Msg == "短信验证码错误") {
                    $("#cellPhoneNumber").val(onekeyUserInfor.CellPhoneNumber);
                    boolNeedCode = true;
                    //$('#codeImg').attr('src', '/account/validatecode?i=' + new Date().getMilliseconds())
                    //$('#inputImgCode').val('');
                };
            }
            else {
                if (data.Content.IsRegistered == false) {
                    if (window.J_RegSuccessCallback) {//jinritoutiao推广注册回调
                        window.J_RegSuccessCallback(data.Content.UserId);
                    };
                    if (window.BdSite_RegSuccessCallback) {
                        window.BdSite_RegSuccessCallback({
                            //必须包含type,并且type的值必须为 tel,sms,msg,book,download
                            type: "book",
                            //页面转化元素id
                            comp_id: "oneKeyBtnSubmit"
                        })
                    }
                    $.ajax({
                        type: 'GET',
                        url: '/common/statics.cshtml?t=' + new Date().getMilliseconds()
                    });
                };
                postData.Head.Token = data.Content.Token;

                $.ajax({
                    type: 'POST', dataType: 'json',
                    url: '/account/UserInfo',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    data: JSON.stringify({ "Head": { "Version": "1.0", "Token": data.Content.Token, "AppType": config.appType } }),
                    contentType: 'application/json',
                    error: function (data, status, headers, config) {
                        msgTip(status);
                    },
                    success: function (data) {
                        userInfor = $.extend(userInfor, data.Content);
                        userInfor.UserID = data.Content.UserID;
                        userInfor.Nickname = userInfor.UserName;
                        userInfor.Token = postData.Head.Token;
                        userInfor.Identity = onekeyUserInfor.Identity;
                        userInfor.IdentityName = onekeyUserInfor.IdentityName;
                        userInfor.Account = onekeyUserInfor.CellPhoneNumber;
                        userInfor.CellPhoneNumber = onekeyUserInfor.CellPhoneNumber;
                        onekeyUserInfor.Account = onekeyUserInfor.CellPhoneNumber;
                        Storage.put('userInfor', userInfor);

                        LoginSearchInfor.LoanAmount = data.Content.LoanAmount;
                        LoginSearchInfor.IDType = userInfor.Identity;
                        localStorage.setItem('LoginSearchInfor', JSON.stringify(LoginSearchInfor));

                        for (i = 0; i < unLoginSearchInfor.length; i++) {
                            if (unLoginSearchInfor[i].Key == 'LoanAmount') {
                                unLoginSearchInfor[i].Value = data.Content.LoanAmount;
                            };
                            if (unLoginSearchInfor[i].Key == 'Identity') {
                                unLoginSearchInfor[i].Value = userInfor.Identity;
                            };
                        };
                        localStorage.setItem('unLoginSearchInfor', JSON.stringify(unLoginSearchInfor));
                        data.Content.Token = token;
                        if (isLogin == false) {
                            unLoginSearchInforApply(data);
                        };
                        //if (isLogin == false && userInfor.LoanAmount<=2) {
                        //    setTimeout(assessEnter("smallloan"), 10);
                        //}else{
                        setTimeout(assessEnter, 10);
                        //}
                    }
                });
            }
        }
    })
};

//贷款额度进度条
function LoanMoneySlider(minNum, maxNum, fromMinNum, fromNum) {
    if (Storage.Cookies.get('onekeyUserInforLoanMoney') != '') {
        onekeyUserInfor.LoanMoney = JSON.parse(Storage.Cookies.get('onekeyUserInforLoanMoney'))[saveInforName];
    } else {
        onekeyUserInfor.LoanMoney = 10;
    };
    if (fromNum == undefined) {
        fromNum = 10;
    };
    $('#rangeIpt').ionRangeSlider({
        type: "single",
        min: minNum,
        max: maxNum,
        from: fromNum,
        step: 1,
        from_min: fromMinNum,
        grid: true,
        grid_num: 6,
        hide_min_max: true,
        prefix: "<span>金额</span>",
        postfix: "万",
        onStart: function (data) {
            $('.irs-slider.single').append('<span class="imgBtn"></span>');
            $('.rangeBox .js-grid-text-6').html('其他');
            $('.rangeBox .irs-bar-edge').before('<span class="irs-bar-edge-pre"></span>');
        },
        onChange: function (data) {
            fromNum = data.from;
            onekeyUserInfor.LoanMoney = data.from;
            if (data.from < data.min + 2) {
                $('.rangeBox .irs-single').addClass('left');
            } else if (data.from > data.max - 3) {
                $('.rangeBox .irs-single').addClass('right');
            } else {
                $('.rangeBox .irs-single').removeClass('left');
                $('.rangeBox .irs-single').removeClass('right');
            }
            if (data.from == data.max) {
                $('#selectMoney').show();
                $("#selectMoney").find("li").removeClass("color-red");
                if ($("#modelli" + onekeyUserInfor.LoanMoney) && $("#modelli" + onekeyUserInfor.LoanMoney).length > 0) {
                    $("#modelli" + onekeyUserInfor.LoanMoney).addClass("color-red");
                }
            }
        },
        onFinish: function (data) {
            if (fromNum > maxNum - 3) {
                $('.rangeBox .irs-single').addClass('right');
                $('.rangeBox .irs-single').html('<span>金额</span>' + fromNum + '万');
            } else if (fromNum < minNum + 2) {
                $('.rangeBox .irs-single').addClass('left');
            }
        }
    });
    $rangeIpt = $("#rangeIpt").data("ionRangeSlider");
}
//选择城市调整保存已填信息
; function SaveInfor(savename) {
    var LoanMoneyNum = onekeyUserInfor.LoanMoney;
    onekeyUserInforLoanMoney[savename] = LoanMoneyNum;
    Storage.Cookies.put('onekeyUserInforLoanMoney', onekeyUserInforLoanMoney, 0);
};